<?php

class ActionIndex extends BaseAction
{
    public function run()
    {

        $iUserId = Yii::app()->user->id;
        $aTariffPlans = TariffPlan::model()->getAvailableTariffPlans($iUserId);

        if(Yii::app()->request->getIsPostRequest())
        {
            $sAction = Yii::app()->request->getParam('do_action');

            if($sAction == 'talks')
            {
                $iBidId = Yii::app()->request->getParam('bid_id');
                $oBid = Bid::model()->findByPk($iBidId);

                if($oBid)
                {
                    $oBid->is_like = 1;

                    if($oBid->update())
                    {
                        $oLot = Lot::model()->findByPk($oBid->lot_id);

                        $oLot->status = 'talks';

                        if($oLot->update())
                        {
                            $oUser = User::model()->findByPk($oBid->user_id);

                            $oEmailNotification = new EmailNotification();
                            $oEmailNotification->sendEmailToUserBeginTalks($oUser, $oBid, $oLot);

                            Yii::app()->user->setFlash('my_lots_manage', 'Вы вступили в переговоры по лоту #'.$oLot->lot_number.'.');
                            Yii::app()->controller->redirect(Yii::app()->controller->createUrl('/profile/my_lots'));
                        }
                    }
                }
            }

            if($sAction == 'close')
            {
                $iLotId = Yii::app()->request->getParam('lot_id');
                $oLot = Lot::model()->findByPk($iLotId);
                
                if($oLot)
                {
                    $oLot->status = 'closed';
                    if($oLot->update())
                    {
                        Yii::app()->user->setFlash('my_lots_manage', 'Лот #'.$oLot->lot_number.' закрыт.');
                        Yii::app()->controller->redirect(Yii::app()->controller->createUrl('/profile/my_lots'));
                    }
                }
            }

            
        }

        $this->template->bCanAdd = (count($aTariffPlans) > 0);
        $this->template->aLots = Lot::model()->getMyLots($iUserId);
    }
}